/*

 * Copyright (c) 2014, Oracle America, Inc.

 * All rights reserved.

 *

 * Redistribution and use in source and binary forms, with or without

 * modification, are permitted provided that the following conditions are met:

 *

 *  * Redistributions of source code must retain the above copyright notice,

 *    this list of conditions and the following disclaimer.

 *

 *  * Redistributions in binary form must reproduce the above copyright

 *    notice, this list of conditions and the following disclaimer in the

 *    documentation and/or other materials provided with the distribution.

 *

 *  * Neither the name of Oracle nor the names of its contributors may be used

 *    to endorse or promote products derived from this software without

 *    specific prior written permission.

 *

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE

 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR

 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF

 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN

 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)

 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF

 * THE POSSIBILITY OF SUCH DAMAGE.

 */

package org.openjdk.jmh.samples;


import org.openjdk.jmh.annotations.Benchmark;

import org.openjdk.jmh.annotations.BenchmarkMode;

import org.openjdk.jmh.annotations.Level;

import org.openjdk.jmh.annotations.Measurement;

import org.openjdk.jmh.annotations.Mode;

import org.openjdk.jmh.annotations.Scope;

import org.openjdk.jmh.annotations.Setup;

import org.openjdk.jmh.annotations.State;

import org.openjdk.jmh.annotations.Warmup;

import org.openjdk.jmh.runner.Runner;

import org.openjdk.jmh.runner.RunnerException;

import org.openjdk.jmh.runner.options.Options;

import org.openjdk.jmh.runner.options.OptionsBuilder;


import java.util.LinkedList;

import java.util.List;


@State(Scope.Thread)

public class JMHSample_26_BatchSize {


 /*

  * Sometimes you need to evaluate operation which doesn't have

  * the steady state. The cost of a benchmarked operation may

  * significantly vary from invocation to invocation.

  *

  * In this case, using the timed measurements is not a good idea,

  * and the only acceptable benchmark mode is a single shot. On the

  * other hand, the operation may be too small for reliable single

  * shot measurement.

  *

  * We can use "batch size" parameter to describe the number of

  * @Benchmark invocations to do per one "shot" without looping the method

  * manually and protect from problems described in JMHSample_11_Loops.

  * If there are any @Setup/@TearDown(Level.Invocation), they still run

  * per each @Benchmark invocation.

  */


 /*

  * Suppose we want to measure insertion in the middle of the list.

  */


 List<String> list = new LinkedList<>();


 @Benchmark

 @Warmup(iterations = 5, time = 1)

 @Measurement(iterations = 5, time = 1)

 @BenchmarkMode(Mode.AverageTime)

 public List<String> measureWrong_1() {

  list.add(list.size() / 2, "something");

  return list;

 }


 @Benchmark

 @Warmup(iterations = 5, time = 5)

 @Measurement(iterations = 5, time = 5)

 @BenchmarkMode(Mode.AverageTime)

 public List<String> measureWrong_5() {

  list.add(list.size() / 2, "something");

  return list;

 }


 /*

  * This is what you do with JMH.

  */

 @Benchmark

 @Warmup(iterations = 5, batchSize = 5000)

 @Measurement(iterations = 5, batchSize = 5000)

 @BenchmarkMode(Mode.SingleShotTime)

 public List<String> measureRight() {

  list.add(list.size() / 2, "something");

  return list;

 }


 @Setup(Level.Iteration)

 public void setup(){

  list.clear();

 }


 /*

  * ============================== HOW TO RUN THIS TEST: ====================================

  *

  * You can see completely different results for measureWrong_1 and measureWrong_5; this

  * is because the workload has no steady state. The result of the workload is dependent

  * on the measurement time. measureRight does not have this drawback, because it measures

  * the N invocations of the test method and measures it's time.

  *

  * We measure batch of 5000 invocations and consider the batch as the single operation.

  *

  * You can run this test:

  *

  * a) Via the command line:

  *    $ mvn clean install

  *    $ java -jar target/benchmarks.jar JMHSample_26 -f 1

  *

  * b) Via the Java API:

  *    (see the JMH homepage for possible caveats when running from IDE:

  *      http://openjdk.java.net/projects/code-tools/jmh/)

  */


 public static void main(String[] args) throws RunnerException {

  Options opt = new OptionsBuilder()

          .include(JMHSample_26_BatchSize.class.getSimpleName())

          .forks(1)

          .build();


  new Runner(opt).run();

 }

// 本例介绍了 batchSize，指定一次迭代方法需要执行 batchSize 次
}

